local assets = {

    Asset("ANIM", "anim/yuemiheng_equipment.zip"),
    Asset("IMAGE", "images/inventoryimages/yuemiheng_hat.tex"),
    Asset("ATLAS", "images/inventoryimages/yuemiheng_hat.xml")
}

local prefabs = {}
local function onequiphat(inst, owner)
    owner.AnimState:OverrideSymbol("swap_hat", "yuemiheng_equipment", "000000")
    owner.AnimState:OverrideSymbol("hair", "yuemiheng_equipment", "00000000")
    owner.AnimState:OverrideSymbol("hair_hat", "yuemiheng_equipment", "0000000")

    owner.AnimState:Show("HAT")
    owner.AnimState:Show("HAT_HAIR")

    if owner:HasTag("player") then
        owner.AnimState:Hide("HEAD")
        owner.AnimState:Show("HEAD_HAT")
    end
    if inst.components.fueled ~= nil then
        inst.components.fueled:StartConsuming()
    end

    inst.Light:Enable(true)
end

local function onunequiphat(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_hat", "yuemiheng_equipment",
        "000000")
    owner.AnimState:ClearOverrideSymbol("hair", "yuemiheng_equipment",
        "00000000")
    owner.AnimState:ClearOverrideSymbol("hair_hat", "yuemiheng_equipment",
        "0000000")
    owner.AnimState:Hide("HAT")
    owner.AnimState:Hide("HAT_HAIR")

    if owner:HasTag("player") then
        owner.AnimState:Show("HEAD")
        owner.AnimState:Hide("HEAD_HAT")
    end

    if inst.components.fueled ~= nil then
        inst.components.fueled:StopConsuming()
    end

    inst.Light:Enable(false)
end

local function onfinished(inst)
    inst:Remove()
end

local function onsave(inst,data)
    data.ymh_hat_gem_num = inst.ymh_hat_gem_num
end

local function onpreload(inst,data)
    inst.ymh_hat_gem_num = data and data.ymh_hat_gem_num

    if inst.ymh_hat_gem_num and inst.ymh_hat_gem_num >= 2 then
        if not inst:HasTag('ymh_hat_upgraded') then
            inst:AddTag('ymh_hat_upgraded')
        end

        if inst.components.finiteuses then
            inst:RemoveComponent('finiteuses')
        end
    end
end

local function fn(Sim)
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddLight()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("yuemiheng_hat")
    inst.AnimState:SetBuild("yuemiheng_equipment")
    inst.AnimState:PlayAnimation("idle", true)

    inst:AddTag("hat")
    inst:AddTag("hide")

    inst.Light:SetFalloff(0.5)
    inst.Light:SetIntensity(.8)
    inst.Light:SetRadius(TUNING.MOD_YUEMIHENG.HAT.LIGHT_RADIUS)
    inst.Light:SetColour(252/255, 212/255, 28/255)
    inst.Light:Enable(false)

    inst.entity:SetPristine()

    inst:AddTag('yuemiheng_hat')

    if not TheWorld.ismastersim then return inst end

    inst.ymh_hat_gem_num = 0

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "yuemiheng_hat"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/yuemiheng_hat.xml"

    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
    inst.components.equippable:SetOnEquip(onequiphat)
    inst.components.equippable:SetOnUnequip(onunequiphat)

    -- inst:AddComponent('armor')
    -- inst.components.armor:InitCondition(TUNING.MOD_YUEMIHENG.HAT.FINITEUSE, TUNING.MOD_YUEMIHENG.HAT.ABSORB)
    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(TUNING.MOD_YUEMIHENG.HAT.FINITEUSE)
    inst.components.finiteuses:SetUses(TUNING.MOD_YUEMIHENG.HAT.FINITEUSE)
    inst.components.finiteuses:SetOnFinished(onfinished)



    inst:AddComponent("rechargeable")
    inst.components.rechargeable:SetChargeTime(TUNING.MOD_YUEMIHENG.HAT.AVOID_DMG_INTERVAL)
    -- inst.components.rechargeable:SetOnDischargedFn(function(inst) end)
    -- inst.components.rechargeable:SetOnChargedFn(function (inst)

    -- end)

    MakeHauntableLaunchAndPerish(inst)

    inst.OnSave = onsave
    inst.OnPreLoad = onpreload
    return inst
end

return Prefab("yuemiheng_hat", fn, assets, prefabs)
